﻿#ifndef MIDDLEWARE_LOGGER_H
#define MIDDLEWARE_LOGGER_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <string.h>

#if defined(_WIN32)
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#else
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif

#define LogLevel_VERBOSE 63
#define LogLevel_DEBUG 31
#define LogLevel_INFO 15
#define LogLevel_WARN 7
#define LogLevel_ERROR 3
#define LogLevel_FATAL 1

extern int _LogLevel;

#define LOG_VERBOSE(fmt, ...) do { if(_LogLevel & 32) logger_log("VERBOSE", __FILENAME__, __LINE__, fmt, ##__VA_ARGS__); } while(0)
#define LOG_DEBUG(fmt, ...) do { if(_LogLevel & 16) logger_log("DEBUG", __FILENAME__, __LINE__, fmt, ##__VA_ARGS__);} while(0)
#define LOG_INFO(fmt, ...) do { if(_LogLevel & 8) logger_log("INFO", __FILENAME__, __LINE__, fmt, ##__VA_ARGS__); } while(0)
#define LOG_WARN(fmt, ...) do { if(_LogLevel & 4) logger_log("WARN", __FILENAME__, __LINE__, fmt, ##__VA_ARGS__); } while(0)
#define LOG_ERROR(fmt, ...) do { if(_LogLevel & 2) logger_log("ERROR", __FILENAME__, __LINE__, fmt, ##__VA_ARGS__); } while(0)
#define LOG_FATAL(fmt, ...) do { if(_LogLevel & 1) logger_log("FATAL", __FILENAME__, __LINE__, fmt, ##__VA_ARGS__); } while(0)

int logger_init();

void logger_log(const char *levelC, const char *file, int line, const char *fmt, ...);

#ifdef __cplusplus
}   /** extern "C" */
#endif

#endif /** MIDDLEWARE_LOGGER_H */
